package com.blrs.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.blrs.common.annotation.Anonymous;
import com.blrs.common.constant.HttpStatus;
import com.blrs.common.core.page.PageDomain;
import com.blrs.common.core.page.TableSupport;
import com.blrs.common.utils.SecurityUtils;
import com.blrs.common.utils.page.PaginationUtil;
import com.blrs.domain.QualificationSwpplier;
import com.blrs.domain.vo.QualificationExcel;
import com.blrs.service.qualification.IQualificationInfoService;
import com.blrs.system.service.ISysDeptService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import com.blrs.common.annotation.Log;
import com.blrs.common.core.controller.BaseController;
import com.blrs.common.core.domain.AjaxResult;
import com.blrs.common.enums.BusinessType;
import com.blrs.domain.QualificationInfo;
import com.blrs.common.utils.poi.ExcelUtil;
import com.blrs.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;
/**
 * 资质产品管理Controller
 * 
 * @author ruoyi
 * @date 2023-08-07
 */
@RestController
@RequestMapping("/qualificationInfo/qualification")
public class QualificationInfoController extends BaseController
{
    @Autowired
    private IQualificationInfoService qualificationInfoService;

    /**
     * 查询资质产品管理列表
     */
    //@PreAuthorize("@ss.hasPermi('qualificationInfo:qualification:list')")
    @GetMapping("/list")
    public TableDataInfo list(QualificationInfo qualificationInfo, @RequestParam Integer pageNum, @RequestParam Integer pageSize)
    {
        startPage();
        List<QualificationInfo> combinedList = qualificationInfoService.selectQualificationInfoList(qualificationInfo);
//        int start = (pageNum - 1) * pageSize;
//        int end = Math.min(start + pageSize, combinedList.size());
//
//        List<QualificationInfo> pageList = combinedList.subList(start, end);
//        TableDataInfo rspData = new TableDataInfo();
//        rspData.setCode(HttpStatus.SUCCESS);
//        rspData.setMsg("查询成功");
//        rspData.setRows(pageList);
//        rspData.setTotal(new PageInfo(combinedList).getTotal());
//        return rspData;
        return getDataTable(combinedList);
    }

    /**
     * 公共端查询所有已上架的产品列表
     * @param qualificationInfo
     * @return
     */
    @Anonymous
    @GetMapping("/getList")
    public TableDataInfo getList(QualificationInfo qualificationInfo){
        startPage();
        List<QualificationInfo> qualificationInfoList=qualificationInfoService.selectQualificationInfoListByListing(qualificationInfo);
        return getDataTable(qualificationInfoList);
    }

    /**
     * 查询公共端所上架的产品信息以及相关企业信息
     * @return
     */
    @GetMapping("/getInfo/{id}")
    @Anonymous
    public AjaxResult getInfoAndOrg(@PathVariable("id") String id)
    {
        return success(qualificationInfoService.selectQualificationInfoByIdAndOrg(id));
    }


    @GetMapping("/listOrgId")
    public TableDataInfo listOrgId(QualificationInfo qualificationInfo)
    {
        startPage();
        List<QualificationInfo> qualificationInfoList = qualificationInfoService.selectQualificationInfoListByOrgId(qualificationInfo);
        return getDataTable(qualificationInfoList);
    }

    @GetMapping("/changelist")
    public TableDataInfo changelist(QualificationInfo qualificationInfo)
    {
        startPage();
        List<QualificationInfo> list = qualificationInfoService.selectQualificationInfoChangeList(qualificationInfo);
        return getDataTable(list);
    }

    @GetMapping("/listAll")
    public TableDataInfo listAll(QualificationInfo qualificationInfo)
    {
        startPage();
        List<QualificationInfo> list = qualificationInfoService.selectQualificationInfoListAll(qualificationInfo);
        return getDataTable(list);
    }

    /**
     * 导出资质产品管理列表
     */
//    @PreAuthorize("@ss.hasPermi('qualificationInfo:qualification:export')")
//    @Log(title = "资质产品管理", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, QualificationInfo qualificationInfo)
//    {
//        List<QualificationInfo> list=null;
//       if (SecurityUtils.getOrgId().equals(100L)){
//          list = qualificationInfoService.selectQualificationInfoListAll(qualificationInfo);
//       }else {
//           list = qualificationInfoService.selectQualificationInfoList(qualificationInfo);
//       }
//        ExcelUtil<QualificationInfo> util = new ExcelUtil<QualificationInfo>(QualificationInfo.class);
//        util.exportExcel(response, list, "资质产品管理数据");
//    }

    /**
     * 获取资质产品管理详细信息
     */
    //@PreAuthorize("@ss.hasPermi('qualificationInfo:qualification:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(qualificationInfoService.selectQualificationInfoById(id));
    }


    /**
     * 选择主营产品
     */
    @GetMapping("/changeById/{id}")
    public AjaxResult changeById(@PathVariable("id") String id){
        return success(qualificationInfoService.changeById(id));
    }

    /**
     * 新增资质产品管理
     */
    //@PreAuthorize("@ss.hasPermi('qualificationInfo:qualification:add')")
    @Log(title = "资质产品管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QualificationInfo qualificationInfo)
    {
        int i = qualificationInfoService.insertQualificationInfo(qualificationInfo);
        if (i>0){
            return success("添加成功");
        }
        return error("添加失败，有重复数据导致");
    }
    /**
     * 获取产品类别动态生成的编号
     */
    @GetMapping("/getNo/{no}/{id}")
    public String getNo(@PathVariable(name = "no")String no,@PathVariable(name = "id",required = false)String id){
       return qualificationInfoService.selectQualificationNo(no,id);
    }

    /**
     * 修改资质产品管理
     */
    //@PreAuthorize("@ss.hasPermi('qualificationInfo:qualification:edit')")
    @Log(title = "资质产品管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QualificationInfo qualificationInfo)
    {
        int i = qualificationInfoService.updateQualificationInfo(qualificationInfo);
        if (i>0){
            return toAjax(i);
        }else {
            return error("添加失败，有重复数据导致");
        }

    }
    /**
     * 上架/下架
     */
    @GetMapping("/approve/{id}/{status}")
     public AjaxResult approve(@PathVariable(name = "id")String [] id,@PathVariable(name = "status")String status){
        return toAjax(qualificationInfoService.approve(id,status));
    }
    /**
     * 删除资质产品管理
     */
    //@PreAuthorize("@ss.hasPermi('qualificationInfo:qualification:remove')")
    @Log(title = "资质产品管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(qualificationInfoService.deleteQualificationInfoByIds(ids));
    }
    /**
     * 导出资质产品模板
     */
   @PostMapping("/importTemplate")
   public void importTemplate(HttpServletResponse response)
    {
       ExcelUtil<QualificationExcel> util = new ExcelUtil<QualificationExcel>(QualificationExcel.class);
       util.importTemplateExcel(response, "资质产品数据");
  }

    @Log(title = "导入资质产品", businessType = BusinessType.IMPORT)
    //@PreAuthorize("@ss.hasPermi('qualificationInfo:qualification:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport)
    {
        ExcelUtil<QualificationInfo> util = new ExcelUtil<QualificationInfo>(QualificationInfo.class);
        List<QualificationInfo> list = null;
        try {
            list = util.importExcel(file.getInputStream());
            for (QualificationInfo qualificationInfo : list) {
                qualificationInfo.setProductStatus("未上架");
                qualificationInfo.setExamineStatus("未审核");
                qualificationInfo.setIsChange(0);
                qualificationInfo.setClassificationId("650c44e1-b575-4b62-9d54-1ba9d1e8abb7");
            }
        } catch (Exception e) {
            return error("导入数据出现异常");
        }
        if ( CollectionUtils.isEmpty(list)){
           return error("导入失败");
       }
        try {
          return qualificationInfoService.importExcel(list);
        } catch (Exception e) {
            System.out.println(e.getMessage()+"测试");
            return error("导入失败,有重复数据");
        }
    }

    /**
     * 查询组织机构，以树结构返回
     */
    @GetMapping("/orgTree")
    public AjaxResult treeHonor(){
        return qualificationInfoService.selectQualificationInfoByType();
    }

    /**
     * 发布
     */
    @GetMapping("/release/{id}")
    public AjaxResult release(@PathVariable String[] id){
        return qualificationInfoService.releaseProduct(id);
    }

    /**
     * 审核
     */
    @PostMapping("/examine")
    public AjaxResult examine(@RequestBody QualificationInfo qualificationInfo){
        return qualificationInfoService.updateQualificationInfoExamine(qualificationInfo);
    }

    /**
     * 撤销审核
     * @param id
     * @return
     */
    @GetMapping("/revoke/{id}")
    public AjaxResult revoke(@PathVariable String id)
    {
        return toAjax(qualificationInfoService.revokeProductById(id));
    }

    /**
     * 停用/启用
     */
    @GetMapping("/apply/{id}")
    public AjaxResult apply(@PathVariable(name = "id")String id){
        return toAjax(qualificationInfoService.updateQualificationInfoApply(id));
    }

//    /**
//     * 供应商产品统计
//     */
//    @GetMapping("/supplierCount")
//    public  TableDataInfo supplierCount(QualificationSwpplier qualificationSwpplier){
//        startPage();
//        List<QualificationSwpplier> list = qualificationInfoService.selectQualificationInfoSupplierCount(qualificationSwpplier);
//        return getDataTable(list);
//    }

}
